package com.jyxd.web.service.assessment.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jyxd.web.dao.basic.VitalSignAlarmDao;
import com.jyxd.web.dao.basic.VitalSignAlarmPlusDao;
import com.jyxd.web.data.basic.VitalSignAlarm;
import com.jyxd.web.data.dto.HttpResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;

@Service
@Transactional
public class VitalSignAlarmService {

    @Autowired
    private VitalSignAlarmDao vitalSignAlarmDao;

    @Resource
    private VitalSignAlarmPlusDao vitalSignAlarmPlusDao;

    public boolean insert(VitalSignAlarm vitalSignAlarm){
        return vitalSignAlarmDao.insert(vitalSignAlarm);
    }

    public boolean update(VitalSignAlarm vitalSignAlarm){
        return vitalSignAlarmDao.update(vitalSignAlarm);
    }

    public VitalSignAlarm queryData(String id){
        return vitalSignAlarmDao.queryData(id);
    }

    public List<VitalSignAlarm> queryList(Map<String,Object> map){
        return vitalSignAlarmDao.queryList(map);
    }

    public int queryNum(Map<String,Object> map){return vitalSignAlarmDao.queryNum(map);}

    /**
     * 系统设置--报警设置--查询指标名称
     * @param map
     * @return
     */
    public VitalSignAlarm queryDataByType(Map<String,Object> map){
        return vitalSignAlarmDao.queryDataByType(map);
    }

    public HttpResult setAlarm(List<VitalSignAlarm> vitalSignAlarmList) {
        errorIfEmpty(vitalSignAlarmList, "参数为空");
        for (int i = 0; i < vitalSignAlarmList.size(); i++) {
            VitalSignAlarm vitalSignAlarm = vitalSignAlarmList.get(i);
            VitalSignAlarm vitalSignAl = vitalSignAlarmPlusDao.selectOne(new QueryWrapper<VitalSignAlarm>().eq("setting_type", vitalSignAlarm.getSettingType()));
            if(ObjectUtils.isEmpty(vitalSignAl)){
                vitalSignAlarmPlusDao.insert(vitalSignAlarm);
            }else{
                vitalSignAlarmPlusDao.update(vitalSignAl,new QueryWrapper<VitalSignAlarm>().eq("setting_type", vitalSignAlarm.getSettingType()));
            }
        }

        return new HttpResult(200,"报警设置成功");
    }
}
